-- Rogue Rage
-- Copyright (C) 2012 Darren Grey
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.


require "engine.class"
--local TooltipsData = require "mod.class.interface.TooltipsData"
local Button = require "engine.ui.Button"
local Mouse = require "engine.Mouse"

--module(..., package.seeall, class.make)
--module(..., package.seeall, class.inherit(TooltipsData))
module(..., package.seeall, class.make)

function _M:init()
	self.font = core.display.newFont("/data/font/Monaco.TTF", 40)
	self.font_h = self.font:lineSkip()

	self.mouse = Mouse.new()

	local gw, gh = core.display.size()
	self:resize(1, gh - 80, gw, 80)
	
end

--- Resize the display area
function _M:resize(x, y, w, h)
	self.display_x, self.display_y = x, y
	self.mouse.delegate_offset_x = x
	self.mouse.delegate_offset_y = y
	self.w, self.h = w, h
	self.font_w = self.font:size(" ")
   self.surface_line = core.display.newSurface(w, self.font_h)
	self.bars_x = self.font_w * 9
	self.bars_w = self.w - self.bars_x - 5
	self.surface = core.display.newSurface(w, h)
	self.texture, self.texture_w, self.texture_h = self.surface:glTexture()

	self.items = {}
end

function _M:mouseTooltip(text, w, h, x, y, click)
	local gw, gh = core.display.size()
	local mx, my, button = core.mouse.get()
	
	self.mouse:registerZone(x, y, 260, h, function(button, mx, my, xrel, yrel, bx, by, event)
		game.tooltip_x, game.tooltip_y = 1, 1; game:tooltipDisplayAtMap(mx+20, my+gh-150, text)
		if click and event == "button" and button == "left" then
			click()
		end
	end)
end

--[[function _M:mouseTooltip(text, w, h, x, y)
	game.tooltip:displayAtMap(nil, nil, w, h, text)
end--]]

--[[function _M:makeTexture(text, x, y, r, g, b, max_w)
	local s = self.surface
	s:drawColorStringBlended(self.font, text, x, y, r, g, b, true, max_w)
	return self.font:size(text)
end--]]

function _M:makeTexture(text, x, y, r, g, b)
	local s = self.surface
	--s:erase(0, 0, 0, 0)
	s:drawColorStringBlended(self.font, text, x, y, r, g, b, true)

	local item = { s:glTexture() }
	item.x = x
	item.y = y
	item.w = self.w
	item.h = self.font_h
	self.items[#self.items+1] = item

	return item.w, item.h, item.x, item.y
end

-- Displays the stats
function _M:display()
	local player = game.player
	if not player or not player.changed or not game.level then return end

	self.mouse:reset()
	self.items = {}

	local s = self.surface
	s:erase(0, 0, 0, 0)

    local gw, gh = core.display.size()
	local w = 15
	local h = 5
	
	self.TOOLTIP_Crit = ("#FIREBRICK#Criticals#LAST#\nYour critical rate shows the chance of landing a super awesome critical on your next attack.  Criticals always hit and ignore armour.  Your critical rate goes up when not attacking or when being attacked.")

	self.TOOLTIP_Paused = ("#STEEL_BLUE#Your tasty tasty stats:#LAST#\nLife:  #LIGHT_BLUE#%d#LAST#/#LIGHT_BLUE#%d#LAST#  Power:  #LIGHT_BLUE#%d#LAST#  Armour:  #LIGHT_BLUE#%d#LAST#  Aim:  #LIGHT_BLUE#%d#LAST#  Dodge:  #LIGHT_BLUE#%d#LAST#  Speed:  #LIGHT_BLUE#%d#LAST#\n#STEEL_BLUE#But what does it all mean?!#LAST#\nDamage dealt = Power - Armour (min 1)\nChance to hit = 50 + 10*Aim - 10*Dodge (min 10%%)\nSpeed increases by 10%% per point\nLeft click to attack, Right-click to pause, Escape for options.  #DARK_ORCHID#Fuck This Jam!#LAST#"):format(game.player.life,game.player.max_life,game.player.power,game.player.armour,game.player.aim,game.player.dodge,game.player.speed)

	self.TOOLTIP_Score = ("#YELLOW#Score#LAST#\nScore more from feistier jams! Scoring lets you level up, which heals you and increases a random stat.\nCurrent score: #KHAKI#%d#LAST#   High score: #KHAKI#%d#LAST#\nLevel: #KHAKI#%d#LAST#   Score to next level: #KHAKI#%d#LAST#"):format(game.player.score,world.best_score,game.player.level,game.player.score_to_level)
	
	w = 10  h = 0

    --self:mouseTooltip(self.TOOLTIP_Life, self:makeTexture(("Life: #LIGHT_BLUE#%d/%d   "):format(player.life, player.max_life), w, h, 220, 220, 255))
	
	--w = 330
	local red = 120 + 2*game.player.crit
	if red > 255 then red = 255 end
	self:mouseTooltip(self.TOOLTIP_Crit, self:makeTexture(("#FIREBRICK#Crit:#LAST# %s%%"):format(game.player.crit), w, h, red, 0, 0))
   
    --w = 630
	--self:makeTexture(("Pots: %s"):format(game.player.no_potions), w, h, 60, 60, 240)
   
    w = gw/2 - 160
	if game.freeze then self:mouseTooltip(self.TOOLTIP_Paused, self:makeTexture("PAUSED", w, h, 255, 255, 255)) end
	
	w = gw - 550
   
	self:mouseTooltip(self.TOOLTIP_Score, self:makeTexture(("Score: %s (%s)"):format(game.player.score,game.player.score_to_level), w, h, 240, 240, 120))

	s:updateTexture(self.texture)
end

function _M:toScreen()
	self:display()
	self.texture:toScreenFull(self.display_x, self.display_y, self.w, self.h, self.texture_w, self.texture_h)
end
